package org.yihang.annotation.easyui;

import org.yihang.util.MapperGenerator;

import java.lang.reflect.Field;

import static org.yihang.util.MapperGenerator.set;

public enum InputType {
    Date {
        @Override
        public String html(String classVarName, Field f, Input ui, String saveOrUpdate) {
            return "<tr><td>"+ui.label()+" :</td><td><input class=\"easyui-datebox\" name=\"" + f.getName() + "\" style=\"width:"
                    + ui.width() + "px\"></td></tr>";
        }

        @Override
        public String func(String classVarName, Field f, Input ui, String saveOrUpdate) {
            return "";
        }

        @Override
        public String java(String classVarName, Field f, Input ui) {
            if(f.getType().equals(java.util.Date.class)){
                return classVarName+"."+ set(f.getName())+"(java.util.Date.from(LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));";
            } else {
                return classVarName+"."+ set(f.getName())+"(java.time.LocalDate.now());";
            }
        }
    },
    DateTime {
        @Override
        public String html(String classVarName, Field f, Input ui, String saveOrUpdate) {
            return "<tr><td>"+ui.label()+" :</td><td><input class=\"easyui-datetimebox\" name=\"" + f.getName() + "\" style=\"width:"
                    + ui.width() + "px\"></td></tr>";
        }

        @Override
        public String func(String classVarName, Field f, Input ui, String saveOrUpdate) {
            return "";
        }

        @Override
        public String java(String classVarName, Field f, Input ui) {
            if(f.getType().equals(java.util.Date.class)){
                return classVarName+"."+ set(f.getName())+"(new java.util.Date());";
            } else {
                return classVarName+"."+ set(f.getName())+"(java.time.LocalDateTime.now());";
            }
        }
    },
    Combo {
        @Override
        public String html(String classVarName, Field f, Input ui, String saveOrUpdate) {
            return "<tr><td>"+ui.label()+" :</td><td><select id=\"" + classVarName + "_"+saveOrUpdate+"_" + f.getName() + "\" name=\""
                    + f.getName() + "\" data-options=\"valueField:'" + ui.valueField() + "',textField:'"
                    + ui.textField() + "',panelHeight:'auto'\" style=\"width:" + ui.width() + "px\"></select></td></tr>";
        }

        @Override
        public String func(String classVarName, Field f, Input ui, String saveOrUpdate) {
            if("".equals(ui.source())){
                throw new RuntimeException(classVarName +"."+ f.getName() + "的@Input作为combobox 必须提供source属性");
            }
            String func = "$(\"#" + classVarName + "_"+saveOrUpdate+"_" + f.getName() + "\").combobox({data:" + ui.source() + ",editable:false});";
            if("".equals(ui.clientValue())){
                return func;
            }
            return func+"$(\"#" + classVarName + "_"+saveOrUpdate+"_" + f.getName() + "\").combobox(\"select\","+ui.clientValue()+");";
        }

        @Override
        public String java(String classVarName, Field f, Input ui) {
            return "";
        }
    },
    ComboTree {
        @Override
        public String html(String classVarName, Field f, Input ui, String saveOrUpdate) {
            return "<tr><td>"+ui.label()+" :</td><td><select id=\"" + classVarName + "_"+saveOrUpdate+"_" + f.getName() + "\" name=\""
                    + f.getName() + "\" data-options=\"panelHeight:'auto'\" style=\"width:" + ui.width() + "px\"></select></td></tr>";
        }

        @Override
        public String func(String classVarName, Field f, Input ui, String saveOrUpdate) {
            if("".equals(ui.source())){
                throw new RuntimeException(classVarName +"."+ f.getName() + "的@Input作为combotree 必须提供source属性");
            }
            return "$(\"#" + classVarName + "_"+saveOrUpdate+"_" + f.getName() + "\").combotree({data:" + ui.source() + ",editable:false});";
        }

        @Override
        public String java(String classVarName, Field f, Input ui) {
            return "";
        }
    },
    Text {
        @Override
        public String html(String classVarName, Field f, Input ui, String saveOrUpdate) {
            if(ui.readonly()) {
                return "<tr><td>"+ui.label()+" :</td><td><input class=\"easyui-textbox\" name=\"" + f.getName() + "\" style=\"width:"
                        + ui.width() + "px\" data-options=\"readonly:true\"></td></tr>";
            }
            return "<tr><td>"+ui.label()+" :</td><td><input class=\"easyui-textbox\" name=\"" + f.getName() + "\" style=\"width:"
                    + ui.width() + "px\"></td></tr>";
        }

        @Override
        public String func(String classVarName, Field f, Input ui, String saveOrUpdate) {
            return "";
        }

        @Override
        public String java(String classVarName, Field f, Input ui) {
            return "";
        }
    };

    public abstract String html(String classVarName, Field f, Input ui, String saveOrUpdate);

    public abstract String func(String classVarName, Field f, Input ui, String saveOrUpdate);

    public abstract String java(String classVarName, Field f, Input ui);
}
